package cn.zust.itcost.service.impl;

import cn.zust.itcost.service.ExtractService;
import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;
import lombok.extern.slf4j.Slf4j;

import org.junit.Test;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class ExtractServiceImpl implements ExtractService {

    /**
     * 关键词和得分
     * @param text
     * @return
     */
    @Override
    public String extractChineseNouns(String text) {
        List<String> sentences = Arrays.asList(text.split("[，。,.]"));

        List<String> extractedNouns = new ArrayList<>();
        for (String sentence : sentences) {
            if (sentence.contains("熟悉")||sentence.contains("掌握")) {
                extractedNouns.addAll(extractNouns(sentence, "10"));
            }
            else if (sentence.contains("了解")||sentence.contains("学过")) {
                extractedNouns.addAll(extractNouns(sentence, "5"));
            }
            else if (sentence.contains("1年经验")||sentence.contains("1年的经验")) {
                extractedNouns.addAll(extractNouns(sentence, "6"));
            }
            else if (sentence.contains("2年经验")||sentence.contains("2年的经验")) {
                extractedNouns.addAll(extractNouns(sentence, "7"));
            }
            else if (sentence.contains("3年经验")||sentence.contains("3年的经验")) {
                extractedNouns.addAll(extractNouns(sentence, "8"));
            }
            else if (sentence.contains("4年经验")||sentence.contains("4年的经验")) {
                extractedNouns.addAll(extractNouns(sentence, "9"));
            }
            else if (sentence.contains("5年经验")||sentence.contains("5年的经验")) {
                extractedNouns.addAll(extractNouns(sentence, "10"));
            }
            else {
                extractedNouns.addAll(extractNouns(sentence, "3"));
            }
        }

        return String.join(",", extractedNouns);
    }

    private List<String> extractNouns(String text, String prefix) {
        List<Term> terms = HanLP.segment(text);

        List<String> chineseNouns = new ArrayList<>();
        for (Term term : terms) {
            if (isChineseNoun(term)) {
                chineseNouns.add(prefix + term.word);
            }
        }

        return chineseNouns;
    }

    /**
     * 关键词
     * @param text
     * @return
     */
    @Override
    public String extractNouns(String text) {
        List<Term> terms = HanLP.segment(text);

        List<String> chineseNouns = new ArrayList<>();
        for (Term term : terms) {
            if (isChineseNoun(term)) {
                chineseNouns.add(term.word);
            }
        }

        return String.join(",", chineseNouns);
    }

    /**
     * 公用
     * @param term
     * @return
     */
    private boolean isChineseNoun(Term term) {
        return term.nature.startsWith('n') && term.word.matches("[\\u4e00-\\u9fa5]+");
    }

    @Test
    public void testExtractChinese() {
        ExtractService extractService = new ExtractServiceImpl();
        String text = "苹果、香蕉和橙子。熟悉这些水果可以帮助你更好地了解它们。同时，经验丰富的人,对水果的了解会更加深入。";
        String result = extractService.extractNouns(text);
        log.info(String.valueOf(result));
    }

    @Test
    public void testExtractChineseNouns() {
        ExtractService extractService = new ExtractServiceImpl();
        String text = "苹果、香蕉和橙子。熟悉这些水果可以帮助你更好地了解它们。同时，经验丰富的人,对水果的了解会更加深入。";
        String result = extractService.extractChineseNouns(text);
        log.info(String.valueOf(result));
    }

}
